package com.plz.afmt.service.impl;

import com.plz.afmt.dto.SysUserDto;
import com.plz.afmt.dto.SysUserLoginDto;
import com.plz.afmt.entity.SysUserEntity;
import com.plz.afmt.service.SysUserService;
import com.plz.afmt.service.UserService;
import com.plz.afmt.vo.BaseUserLoginVo;
import com.plz.afmt.vo.SysUserVo;
import com.plz.common.constant.CacheConstants;
import com.plz.common.exception.BusinessException;
import com.plz.framework.cache.CacheService;
import com.plz.shrio.service.TokenService;
import com.plz.shrio.utils.UserMangerUtil;
import com.plz.shrio.vo.BaseLoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 2 * @Author: swing
 * 3 * @Date: 2021/6/21 11:24
 * 4
 */
@Service
public class LocalUserServiceImpl extends TokenService {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private CacheService cacheService;

    /**
     * 管理端登录
     *
     * @param dto
     * @return
     */
    public BaseLoginVo login(SysUserLoginDto dto) {
        BaseUserLoginVo result = new BaseUserLoginVo();
        SysUserEntity user = sysUserService.queryByLoginName(dto.getLoginName());
        if (user == null) {
            throw new BusinessException("用户不存在");
        } else if (!user.getPassword().equals(dto.getPassword())) {
            throw new BusinessException("密码错误");
        }
        result.setId(user.getId())
                .setLoginName(user.getLoginName())
                .setUserName(user.getLoginName())
                .setUserType(0);
        String token = generateToken(result);
        cacheService.put(CacheConstants.LOGIN_TOKEN_KEY + result.getId(), token, 21600);
        result.setToken(token);
        return result;
    }

    public BaseLoginVo me(){
        BaseLoginVo currentUser = UserMangerUtil.getCurrentUser();
        return currentUser;
    }
}

